using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components;

namespace Workflows.Components.DB.Orm
{
    public partial class OrmEditor : Form, IComponentEditor 
    {
        private object _OwnerActivityNode;
        private OrmComponent _Orm;
        /// <summary></summary>
        public OrmEditor()
        {
            InitializeComponent();
        }

        private void OrmEditor_Load(object sender, EventArgs e)
        {
            this.cbo_UpdateAction.Items.Clear();
            for(int i=0;i<=7;i++)
            {
                OrmActionType actionType = (OrmActionType) i;
                this.cbo_UpdateAction.Items.Add(actionType.ToString());
            }
        }

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _OwnerActivityNode; }
            set { _OwnerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(OrmComponent); }
        }

        public void LoadSettings(
            GlobalVariable[] globalVars,
            WorkflowComponentBase[] fromComponents, 
            object activityNode, WorkflowComponentBase wfComponent)
        {
            this._OwnerActivityNode = activityNode;
            this._Orm = (OrmComponent) wfComponent;

            this.txt_ConnStr.Text = this._Orm.ConnStr;
            this.btn_RefreshTables_Click(this,new EventArgs());
            if(this.cbo_TblNames.Items.Count >0 && (!string.IsNullOrEmpty(this._Orm.TblName)))
            {
                for(int i=0;i<this.cbo_TblNames.Items.Count;i++)
                {
                    if(this.cbo_TblNames.Items[i].ToString()==this._Orm.TblName)
                    {
                        this.cbo_TblNames.SelectedIndex = i;
                        break;
                    }
                }
            }
            if(this.cbo_Fields.Items.Count >0 && (!string.IsNullOrEmpty(this._Orm.PK)))
            {
                for(int i=0;i<this.cbo_Fields.Items.Count;i++)
                {
                    if(this.cbo_Fields.Items[i].ToString()==this._Orm.PK)
                    {
                        this.cbo_Fields.SelectedIndex = i;
                        break;
                    }
                }
            }
            this.cbo_UpdateAction.SelectedIndex = (int) this._Orm.ActionType;
            this.ck_IdentityInsert.Checked = this._Orm.IsIdentity;

            this.txt_DllFilePath.Text = this._Orm.BindableObjAssemblyFileName;
            if(this.cbo_ObjType.Items.Count>0 && (!string.IsNullOrEmpty(this._Orm.BindableObjTypeFullName)))
            {
                for(int i=0;i<this.cbo_ObjType.Items.Count;i++)
                {
                    if(this.cbo_ObjType.Items[i].ToString()==this._Orm.BindableObjTypeFullName)
                    {
                        this.cbo_ObjType.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        public TestResult Test()
        {
            return new TestResult(true, string.Empty);
        }

        public void SaveSettings()
        {
            this._Orm.ConnStr = this.txt_ConnStr.Text;
            if(this.cbo_TblNames.SelectedIndex >=0)
            {
                this._Orm.TblName = this.cbo_TblNames.SelectedItem.ToString();
            }
            else
            {
                this._Orm.TblName = string.Empty;
            }
            if(this.cbo_Fields.SelectedIndex>=0)
            {
                this._Orm.PK = this.cbo_Fields.SelectedItem.ToString();
            }
            else
            {
                this._Orm.PK = string.Empty;
            }
            this._Orm.ActionType = (OrmActionType) this.cbo_UpdateAction.SelectedIndex;
            this._Orm.IsIdentity = this.ck_IdentityInsert.Checked;

            this._Orm.BindableObjAssemblyFileName = this.txt_DllFilePath.Text;
            if(this.cbo_ObjType.SelectedIndex >=0)
            {
                this._Orm.BindableObjTypeFullName = this.cbo_ObjType.SelectedItem.ToString();
            }
            else
            {
                this._Orm.BindableObjTypeFullName = string.Empty;
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent !=null)
            {
                this.UpdateComponentSettingEvent(this._OwnerActivityNode, this._Orm);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Test_Click(object sender, EventArgs e)
        {

        }

        private void btn_RefreshTables_Click(object sender, EventArgs e)
        {
            this.cbo_TblNames.Items.Clear();
            this.cbo_TblNames.SelectedIndex = -1;
            this.cbo_TblNames.Text = string.Empty;
            if(!string.IsNullOrEmpty(this.txt_ConnStr.Text))
            {
                string[] tblNames = DbUtil.GetTableNames(DataProviderType.MSSQL, string.Empty, this.txt_ConnStr.Text);
                if(tblNames !=null && tblNames.Length>0)
                {
                    foreach(string tblName in tblNames)
                    {
                        this.cbo_TblNames.Items.Add(tblName);
                    }
                }
            }
        }

        private void cbo_TblNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cbo_Fields.Items.Clear();
            this.cbo_Fields.SelectedIndex = -1;
            this.cbo_Fields.Text = string.Empty;
            if(!string.IsNullOrEmpty(this.txt_ConnStr.Text) && this.cbo_TblNames.SelectedIndex>=0)
            {
                TableColumn[] cols =
                    DbUtil.GetTableColumns(DataProviderType.MSSQL, this.txt_ConnStr.Text, string.Empty,
                                           this.cbo_TblNames.SelectedItem.ToString());
                if(cols !=null && cols.Length>0)
                {
                    foreach(TableColumn col in cols)
                    {
                        this.cbo_Fields.Items.Add(col.ColumnName);
                    }
                }
            }
        }

        private void btn_BrowseAssembly_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg=new OpenFileDialog();
            dlg.Filter = "Assembly files(*.dll)|*.dll|All files(*.*)|*.*";
            if(dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                this.txt_DllFilePath.Text = dlg.FileName;
            }
        }

        private void txt_DllFilePath_TextChanged(object sender, EventArgs e)
        {
            this.cbo_ObjType.Items.Clear();
            if(File.Exists(this.txt_DllFilePath.Text))
            {
                Assembly assem = Assembly.LoadFile(this.txt_DllFilePath.Text);
                Type[] types = assem.GetTypes();
                if(types !=null && types.Length>0)
                {
                    foreach(Type type in types)
                    {
                        this.cbo_ObjType.Items.Add(type.FullName);
                    }
                }
            }
        }
    }
}